package com.weixin.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.weixin.entity.Goods;
import com.weixin.service.IGoodsService;
import com.weixin.service.impl.GoodsServiceImpl;
import com.weixin.util.PageSizeUtil;

/**
 * Servlet implementation class GoodsServlet
 */
@WebServlet("/GoodsServlet/*")
public class GoodsServlet extends HttpServlet {
	private static Logger logger = Logger.getLogger(GoodsServlet.class); 
	IGoodsService goodsservice=new GoodsServiceImpl();

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		doGet(request, response);
	}
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path=request.getPathInfo().substring(1);
		if("listAll".equals(path)){
			ListAll(request,response);
		}else if("addGoods".equals(path)){
			AddGoods(request,response);
		}else if("deleteById".equals(path)){
			DeleteById(request,response);
		}else if("preEditGoods".equals(path)){
			PreEditGoods(request,response);
		}else if("editGoods".equals(path)){
			editGoods(request,response);
		}else if("findGoods".equals(path)){
			FindGoods(request,response);
		}else if("findGoodsByChoose".equals(path)){
			FindGoodsByChoose(request,response);
		}
	}
	private void FindGoodsByChoose(HttpServletRequest request, HttpServletResponse response) {
		String choose = request.getParameter("choose");
		String values = request.getParameter("values");
		System.out.println(choose+"条件和值"+values);
		try {
			if("goods_id".equals(choose)){
				List<Goods> goodslist = goodsservice.selectByGoodsId(Integer.valueOf(values));
				request.setAttribute("goodslist", goodslist);
				request.getRequestDispatcher("/page/user/goodsManage/findGoods.jsp").forward(request,response);
			}else if("goods_name".equals(choose)){
				List<Goods> goodslist = goodsservice.selectByName(values);
				request.setAttribute("goodslist", goodslist);
				request.getRequestDispatcher("/page/user/goodsManage/findGoods.jsp").forward(request,response);
			}else if("goods_classification".equals(choose)){
				List<Goods> goodslist = goodsservice.selectByClass(values);
				request.setAttribute("goodslist", goodslist);
				request.getRequestDispatcher("/page/user/goodsManage/findGoods.jsp").forward(request,response);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	private void FindGoods(HttpServletRequest request, HttpServletResponse response) {
		int pageNum=Integer.valueOf(request.getParameter("pageNum"));
		try {
			//商品列表
			List<Goods> goodslist = goodsservice.selectAll(PageSizeUtil.PAGE_SIZE6, pageNum);
			request.setAttribute("goodslist",goodslist);
			//分页所用数据
			Double pageCount=Math.ceil(goodsservice.selectAllTotal()*1.0/PageSizeUtil.PAGE_SIZE6);
			request.setAttribute("pageCount",pageCount);
			request.setAttribute("pageNum",pageNum);
			
			request.getRequestDispatcher("/page/user/goodsManage/findGoods.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private void editGoods(HttpServletRequest request, HttpServletResponse response) {
		int goods_id =Integer.valueOf(request.getParameter("goods_id"));
		String name = request.getParameter("name");
		String classification = request.getParameter("classification");
		String price = request.getParameter("price");
		String unit = request.getParameter("unit");
		String origin = request.getParameter("origin");
		String introduce = request.getParameter("introduce");
		String remark = request.getParameter("remark");
		
		try {
			Goods goods = goodsservice.selectById(goods_id);
			goods.setGoods_name(name);
			goods.setGoods_classification(classification);
			goods.setPrice(Double.parseDouble(price));;
			goods.setUnit(unit);
			goods.setOrigin(origin);
			goods.setIntroduce(introduce);
			goods.setRemark(remark);
			
			if(goodsservice.updateGoods(goods)) {
				logger.error("修改商品："+goods);
				response.getWriter().print(1);
			}else {
				response.getWriter().print(0);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		
	}
	private void PreEditGoods(HttpServletRequest request, HttpServletResponse response) {
		String goods_id = request.getParameter("goods_id");
		System.out.println(goods_id);
		try {
			Goods goods = goodsservice.selectById(Integer.valueOf(goods_id));			
			request.setAttribute("goods", goods);
			request.getRequestDispatcher("/page/user/goodsManage/editGoods.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	private void DeleteById(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("来到删除之处");
		String goods_id = request.getParameter("goods_id");
		try {
			if(goodsservice.deleteById(Integer.valueOf(goods_id))) {
				logger.error("伪删除商品，id："+goods_id);
				response.getWriter().println("1");
			}else{
				response.getWriter().println("0");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
	}
	private void AddGoods(HttpServletRequest request, HttpServletResponse response) {
		System.err.println("到达添加处");
		
		String name = request.getParameter("name");
		String classification = request.getParameter("classification");
		String price = request.getParameter("price");
		String unit = request.getParameter("unit");
		String origin = request.getParameter("origin");
		String introduce = request.getParameter("introduce");
		String remark = request.getParameter("remark");
		
		Goods goods=new Goods();
		goods.setGoods_name(name);
		goods.setGoods_classification(classification);
		goods.setPrice(Double.parseDouble(price));;
		goods.setUnit(unit);
		goods.setOrigin(origin);
		goods.setIntroduce(introduce);
		goods.setRemark(remark);
		
		try {
			if(goodsservice.insertGoods(goods)) {
				logger.error("添加商品："+goods);
				response.getWriter().println("1");
			}else {
				response.getWriter().println("0");
			}			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
			
		}
	}
	private void ListAll(HttpServletRequest request, HttpServletResponse response) {
		try {
			//商品列表
			List<Goods> goodslist = goodsservice.getAll();
			request.setAttribute("goodslist",goodslist);			
			request.getRequestDispatcher("/page/user/goodsManage/goodsList.jsp").forward(request, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


}
